<?php
namespace app\repair\model;
use think\Request;
use app\common\model\Common;
use think\Db;
class Stock extends Common
{
	protected $table = 'mps_stock_repair';
	protected $autoWriteTimestamp  = true;
	protected $createTime = 'addtime';
	protected $updateTime = 'ret_time';
	protected $append = ['state_text','position_text'];

	/**
	 * 获取列表
	 * @param array $where 查询条件
	 * @param string $order 排序条件
	 * @param intger $page 当前页数
	 * @param intger $row 每页条数
	 * @param string $search 关键字检索条件
	 */
	public function listData($where, $order, $page, $row, $search = '')
	{
		if($where === false) return [];
		if( $row == 'limit' ){
			return $this->alias('sr')->field('sr.*')->join('mps_goods g','sr.goods_id = g.goods_id','LEFT')->where($search)->where($where)->order($order)->group('sr.stock_repair_id')->select();
		}
		return $this->alias('sr')->field('sr.*')->join('mps_goods g','sr.goods_id = g.goods_id','LEFT')->where($search)->where($where)->order($order)->page($page, $row)->group('sr.stock_repair_id')->select();
	}
	/**
	 * 获取列表总条数
	 * @param array $where 查询条件
	 * @param string $order 排序条件
	 * @param string $search 关键字检索条件
	 */
	public function listCount($where, $order, $search = '')
	{
		if($where === false) return 0;
		return $this->alias('sr')->join('mps_goods g','sr.goods_id = g.goods_id','LEFT')->where($search)->where($where)->order($order)->group('sr.stock_repair_id')->select();
	}
	/**
	 * 恢复商品库存
	 */
	public function incGoodsNumber($stock_id, $num)
	{
		return Db::name('stock')->where('stock_id',$stock_id)->setInc('number',$num);
	}
	/**
	 * 获取商品属性列表
	 */
	public function get_attribute($fid, $type)
	{
		return Db::name('attribute')->where('category_id',$fid)->where('is_show',0)->where('type',$type)->select();
	}

	/**
	 * 获取器
	 */
	public function getStateTextAttr($value, $data)
	{
		switch($data['state']){
			case 0: return '返库中';break;
			case 1: return '已返库';break;
			default: return '异常';break;
		}
	}
	public function getPositionTextAttr($value, $data)
	{
		switch($data['position']){
			case 0: return '【店铺】';break;
			case 1: return '【仓库】';break;
			default: return '异常';break;
		}
	}
	public function getRetTimeAttr($value)
	{
		return $value > 0 ? date('Y-m-d H:i:s',$value) : '---';
	}

}